package com.lwc.service.impl;

import com.lwc.bean.Order;
import com.lwc.client.StockRemoteClient;
import com.lwc.ex.Assets;
import com.lwc.mapper.OrderMapper;
import com.lwc.mapper.OrderdetailMapper;
import com.lwc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderdetailMapper orderdetailMapper;


    @Autowired
    OrderMapper orderMapper;

    @Autowired
    StockRemoteClient stockRemoteClient;

    @Override
    @Transactional
    public int save(Order order) {
//生成订单
        int rs=orderMapper.save(order);
            Assets.isOk(rs);
            //生成订单明细

        if (order.getOrderdetails()!=null &&order.getOrderdetails().size()>0){

            rs=orderdetailMapper.batchSave(order.getId(),order.getOrderdetails());
            Assets.isOk(rs);
            //扣数量
            order.getOrderdetails().forEach(el->{
                stockRemoteClient.reduceStock(el.getGid(),el.getNum());
            });
        }

        System.out.println("订单已扣除");
        return 0;
    }
}
